package com.sunlands.qdcy.deskmate.config;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.http.HttpHeaders;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import java.text.SimpleDateFormat;

/**
 * @author zhaocg
 * @create 2019/4/19 09:46
 */
@Configuration
@Slf4j
public class WebAppConfigurer extends WebMvcConfigurerAdapter {

    @Bean(name = "jacksonObjectMapper")
    @Primary
    @ConditionalOnMissingBean(ObjectMapper.class)
    public ObjectMapper jacksonObjectMapper(Jackson2ObjectMapperBuilder builder){

        ObjectMapper objectMapper = builder.createXmlMapper(false).build();

        //序列化的时候序列对象的所有属性
        objectMapper.setSerializationInclusion(JsonInclude.Include.ALWAYS);

        //取消时间的转化格式,默认是时间戳,可以取消,同时需要设置要表现的时间格式
        objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
        objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));

        //反序列化的时候如果多了其他属性,不抛出异常
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

        // 允许出现特殊字符和转义符
        objectMapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true);
        // 允许出现单引号
        objectMapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);

        //表示允许无引号包括的字段
        objectMapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);

        return objectMapper;
    }

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowedMethods("*")
                .allowedHeaders("*")
                .allowCredentials(true)
                .exposedHeaders(HttpHeaders.SET_COOKIE).maxAge(3600L);
    }


    public static final long timeout = 5 * 1000;

    public enum WebTimeOutEnum {
        DOWN_ASSERTS_LIABILITY("/upload/avatar-url", 20 * 1000),
        ;

        private String serviceUrl;
        private long timeout;

        WebTimeOutEnum(String serviceUrl, long timeout) {
            this.serviceUrl = serviceUrl;
            this.timeout = timeout;
        }

        public static WebTimeOutEnum url(String url) {
            for (WebTimeOutEnum timeOutEnum : WebTimeOutEnum.values()) {
                if (timeOutEnum.getServiceUrl().equals(url)) {
                    return timeOutEnum;
                }
            }
            return null;
        }

        public String getServiceUrl() {
            return serviceUrl;
        }

        public long getTimeout() {
            return timeout;
        }
    }

}
